﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Net;
using HtmlAgilityPack;
using BusinessLogic.BusinessRules;

namespace IssueCommon
{
    class Htmlhandler
    {
        public static string HandlerHtmlElementPath(string htmlDocContent, SessionUser sessionUser)
        {
            try
            {
                string Targethost = "";
                string TargethostEnableParentPath = "";
                //string Targethost = System.Configuration.ConfigurationSettings.AppSettings["Targethost"] ?? "";

                Targethost = "/news/" + sessionUser.VartualPath + "/";
                TargethostEnableParentPath = "/" + sessionUser.VartualPath + "/";

                string htmldoc = htmlDocContent;
                HtmlDocument htm = new HtmlDocument();
                htm.LoadHtml(htmldoc);
                //构建a标签
                HtmlNodeCollection imgparent = htm.DocumentNode.SelectNodes("//a");
                if (imgparent != null)
                {
                    if (imgparent.Count > 0)
                    {
                        for (int i = 0; i < imgparent.Count; i++)
                        {
                            if (imgparent[i].Attributes["href"] != null)
                            {
                                if (imgparent[i].Attributes["href"].Value.Trim() != "")
                                {
                                    if (!imgparent[i].Attributes["href"].Value.ToLower().Contains("://") &&
                                        (!imgparent[i].Attributes["href"].Value.ToLower().Contains("#") || imgparent[i].Attributes["href"].Value.ToLower().IndexOf("#") != 0) &&
                                        !imgparent[i].Attributes["href"].Value.Contains("(") && (imgparent[i].Attributes["href"].Value.Substring(0, 1) != "/"))
                                    {
                                        imgparent[i].Attributes["href"].Value = imgparent[i].Attributes["href"].Value.Replace("../", "");
                                        imgparent[i].Attributes["href"].Value = (sessionUser.WebSiteLevel==1?Targethost:TargethostEnableParentPath) +
                                                                                imgparent[i].Attributes["href"].Value;
                                    }
                                }
                            }
                        }
                    }
                }
                //构建img标签
                imgparent = htm.DocumentNode.SelectNodes("//img");
                if (imgparent != null)
                {
                    if (imgparent.Count > 0)
                    {
                        for (int i = 0; i < imgparent.Count; i++)
                        {
                            if (imgparent[i].Attributes["src"] != null)
                            {
                                if (imgparent[i].Attributes["src"].Value.Trim() != "")
                                {
                                    if (!imgparent[i].Attributes["src"].Value.ToLower().Contains("http:") && !imgparent[i].Attributes["src"].Value.ToLower().Contains(("/news/" + sessionUser.VartualPath + "/Pcfiles/").ToLower()) && (imgparent[i].Attributes["src"].Value.Substring(0, 1) != "/"))
                                    {
                                        imgparent[i].Attributes["src"].Value = imgparent[i].Attributes["src"].Value.Replace("../", "");
                                        imgparent[i].Attributes["src"].Value = Targethost +
                                                                               imgparent[i].Attributes["src"].Value;
                                    }
                                }
                            }
                        }
                    }
                }

                //构建link标签
                imgparent = htm.DocumentNode.SelectNodes("//link");
                if (imgparent != null)
                {
                    if (imgparent.Count > 0)
                    {

                        for (int i = 0; i < imgparent.Count; i++)
                        {
                            if (imgparent[i].Attributes["href"] != null)
                            {
                                if (imgparent[i].Attributes["href"].Value.Trim() != "")
                                {
                                    if (!imgparent[i].Attributes["href"].Value.ToLower().Contains("http:") && (imgparent[i].Attributes["href"].Value.Substring(0, 1) != "/"))
                                    {
                                        imgparent[i].Attributes["href"].Value = imgparent[i].Attributes["href"].Value.Replace("../", "");
                                        imgparent[i].Attributes["href"].Value = Targethost +
                                                                                imgparent[i].Attributes["href"].Value;
                                    }
                                }
                            }
                        }
                    }
                }

                //构建link标签
                imgparent = htm.DocumentNode.SelectNodes("//script");
                if (imgparent != null)
                {
                    if (imgparent.Count > 0)
                    {
                        for (int i = 0; i < imgparent.Count; i++)
                        {
                            if (imgparent[i].Attributes["src"] != null)
                            {
                                if (imgparent[i].Attributes["src"].Value.Trim() != "")
                                {
                                    if (!imgparent[i].Attributes["src"].Value.ToLower().Contains("http:") && (imgparent[i].Attributes["src"].Value.Substring(0, 1) != "/"))
                                    {
                                        imgparent[i].Attributes["src"].Value = imgparent[i].Attributes["src"].Value.Replace("../", "");
                                        imgparent[i].Attributes["src"].Value = Targethost +
                                                                               imgparent[i].Attributes["src"].Value;
                                    }
                                }
                            }
                        }
                    }
                }

                //构建frame标签
                imgparent = htm.DocumentNode.SelectNodes("//iframe");
                if (imgparent != null)
                {
                    if (imgparent.Count > 0)
                    {
                        for (int i = 0; i < imgparent.Count; i++)
                        {
                            if (imgparent[i].Attributes["src"] != null)
                            {
                                if (imgparent[i].Attributes["src"].Value.Trim() != "")
                                {
                                    if (!imgparent[i].Attributes["src"].Value.ToLower().Contains("http:") && (imgparent[i].Attributes["src"].Value.Substring(0, 1) != "/"))
                                    {
                                        imgparent[i].Attributes["src"].Value = imgparent[i].Attributes["src"].Value.Replace("../", "");
                                        imgparent[i].Attributes["src"].Value =  (sessionUser.WebSiteLevel==1? Targethost : TargethostEnableParentPath) +
                                                                               imgparent[i].Attributes["src"].Value;
                                    }
                                }
                            }
                        }
                    }
                }

                return htm.DocumentNode.OuterHtml;
            }
            catch (Exception ex)
            {
                throw;
            }
        }
    }
}
